﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Text;
using ShopModel;
using System.Data;
using System.Data.SqlClient;


namespace ShopDAO
{
    public class NewsDAO
    {
        private string connectionString;

        private SqlConnection Connection()
        {
            return new SqlConnection(connectionString);
        }

        public NewsDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// (Pobiera newsa do wysłania na podstaiw jego id)Get News objects on the basis of id News.
        /// </summary>
        /// <param name="NewsID">Id newsa do pobrania(Id News for Get.)</param>
        /// <param name="LanguageID">Id języka w jakim wyświetlana jest strona (Language id in which website is displayed.)</param>
        /// <returns>Objekt typu News przechowujący treść newsa(News object containing the whole news.)</returns>
        public News GetNewsByID(int NewsID, int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;
            News result = null;

            command.CommandText = "GetNews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@NewsID", NewsID));
            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if (sqlR.Read())
                {
                    result = new News((int)sqlR["IDnews"], (string)sqlR["Title"], (string)sqlR["Description"], (string)sqlR["Content"], (DateTime)sqlR["NewsDate"]);
                }

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { sqlC.Close(); }

            return result;
        }

        /// <summary>
        /// Pobiera objekt typu news, zaspisuje go w bazie, jeśli istnieje uaktualnia treść newsa (Get News object, saves new news, updates content if it exists.)
        /// </summary>
        /// <param name="NewsToSave">Object typu News przechowujacy trść newsa (News object containing news to be saved.)</param>
        /// <param name="LanguageID">Id języka w jaki wyświetlana jest strona (Language id in which website is displayed.)</param>
        /// <returns>Id of added news.</returns>
        public int SaveNews(News NewsToSave, int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            int result = -1;

            command.CommandText = "SaveNews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@NewsID", NewsToSave.NewsID));
            command.Parameters.Add(new SqlParameter("@Title", NewsToSave.Title));
            command.Parameters.Add(new SqlParameter("@Description", NewsToSave.Description));
            command.Parameters.Add(new SqlParameter("@Content", NewsToSave.Content));
            command.Parameters.Add(new SqlParameter("@NewsDate", NewsToSave.NewsDate));
            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if(sqlR.Read())
                result = (int)sqlR["NewsID"];

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { sqlC.Close(); }

            return result;
        }

        /// <summary>
        /// Usuwa wiadomość z tbali(Removes news from tbale.)
        /// </summary>
        /// <param name="NewsID">Id news'a do usunięcia (News id to be removed.)</param>
        /// <returns>Zwraca 'true' jeśli operacja usuwania zakończona sukcesem (If ‘true’, process of removal succeeded.)</returns>
        public bool DeleteNews(int NewsID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            bool result = false;

            command.CommandText = "DeleteNews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@NewsID", NewsID));

            try
            {
                sqlC.Open();
                int rows = command.ExecuteNonQuery();

                if (rows > 0)
                    result = true;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { sqlC.Close(); }

            return result;
        }

        /// <summary>
        /// Pobiera wszystkie newsy z tableli News(Gets all news form the News table.)
        /// </summary>
        /// <param name="LanguageID">Unikalne id języka w jakim wyświetlana jest strona. (Language id in which website is displayed.)</param>
        /// <returns>Lista objektów typu News przechowująca treść wiadomości. (Lis of News objects containing news content.)</returns>
        public List<News> GetNews(int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;
            List<News> result = new List<News>();

            command.CommandText = "GetNews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                while(sqlR.Read())
                {
                    result.Add(new News((int)sqlR["IDnews"], (string)sqlR["Title"], (string)sqlR["Description"], (string)sqlR["Content"], (DateTime)sqlR["NewsDate"]));
                }

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { sqlC.Close(); }

            return result;
        }
    }
}
